/*
  Copyright 2002-2003 The University of Texas at Austin
  
    Authors: Anthony Thane <thanea@ices.utexas.edu>
             Vinay Siddavanahalli <skvinay@cs.utexas.edu>
    Advisor: Chandrajit Bajaj <bajaj@cs.utexas.edu>

  This file is part of Volume Rover.

  Volume Rover is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  Volume Rover is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with iotree; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// ColorTableInformation.cpp: implementation of the ColorTableInformation class.
//
//////////////////////////////////////////////////////////////////////

#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QMessageBox>

#include "transfermap.h"

using namespace PBVP;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TransferMap::TransferMap()
{
	// just uses the default constructors for color, alpha, and isocontour maps
}

TransferMap::TransferMap(const AlphaMap& alphaMap, const ColorMap& colorMap, const IsocontourMap& isocontourMap)
: m_AlphaMap(alphaMap), m_ColorMap(colorMap), m_IsocontourMap(isocontourMap)
{
	removeSandwichedNodes();
}

TransferMap::TransferMap(const TransferMap& copy)
: m_AlphaMap(copy.getAlphaMap()), m_ColorMap(copy.getColorMap()), m_IsocontourMap(copy.getIsocontourMap())
{

}

TransferMap::~TransferMap()
{

}

void TransferMap::removeSandwichedNodes()
{
	removeSandwichedAlphaNodes();
	removeSandwichedColorNodes();
}

// Keep only the extreme nodes and make them both white.
void TransferMap::resetNodes()
{
	m_ColorMap.resetNodes();
}

const AlphaMap& TransferMap::getAlphaMap() const
{
	return m_AlphaMap;
}

const ColorMap& TransferMap::getColorMap() const
{
	return m_ColorMap;
}

const IsocontourMap& TransferMap::getIsocontourMap() const
{
	return m_IsocontourMap;
}

AlphaMap& TransferMap::getAlphaMap()
{
	return m_AlphaMap;
}

ColorMap& TransferMap::getColorMap()
{
	return m_ColorMap;
}

IsocontourMap& TransferMap::getIsocontourMap()
{
	return m_IsocontourMap;
}

bool TransferMap::saveColorTable(const QString& fileName)
{
	QFileInfo fileInfo(fileName);
	QString extension = fileInfo.suffix();
	QString longName;

	// if no extension, add one
	if (extension.isEmpty()) {
		longName = fileName + ".cmap";
	}
	else {
		longName = fileName;
	}

	QFile file(longName);
	if (!file.open(QIODevice::WriteOnly)) {
		return false;
	}
	QTextStream stream(&file);
	stream << "Colormap file for PBVP\n";
	m_AlphaMap.saveMap(stream);
	m_ColorMap.saveMap(stream);
	m_IsocontourMap.saveMap(stream);
	file.close();

	return true;
}

bool TransferMap::loadColorTable(const QString& fileName)
{
	QFile file(fileName);
	if (!file.open(QIODevice::ReadOnly)) {
		return false;
	}
	QTextStream stream(&file);

	bool ret = loadColorTable( stream );
	file.close();
	return ret;
}

bool TransferMap::loadColorTable(QTextStream& stream)
{
	stream.skipWhiteSpace();
	QString verificationString = stream.readLine(); 
	if (verificationString != "Colormap file for PBVP") {
		return false;
	}
	else {
		m_AlphaMap.loadMap(stream);
		m_ColorMap.loadMap(stream);
		m_IsocontourMap.loadMap(stream);
		return true;
	}
}

void TransferMap::removeSandwichedAlphaNodes()
{
	m_AlphaMap.removeSandwichedNodes();
}

void TransferMap::removeSandwichedColorNodes()
{
	m_ColorMap.removeSandwichedNodes();
}

